import 'package:aichat_flutter_wallet/module_common/widgets/button/public_button.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../utils/util.dart';

enum DialogType {
  PASSWORD,
  RISK_SECURITY,
  // 本地异常
  WALLET_ERROR,
  // 销毁 钱包
  DELETE_WALLET,
  // 服务端 异常
  SERVER_SIDE_ERROR,
  // 云端异常
  CLOUD_SIDE_ERROR,
  // 谷歌授权
  GOOGLE_CLOUD_AUTH,
  // 谷歌授权
  APPLE_CLOUD_AUTH,
}

class KaiBottomTipDialog extends StatefulWidget {
  late DialogType type = DialogType.PASSWORD;
  Function? Cancel;
  Function? Confirm;

  KaiBottomTipDialog({super.key, this.type = DialogType.PASSWORD, this.Cancel, this.Confirm});

  @override
  State<StatefulWidget> createState() {
    return _KaiBottomTipDialogState();
  }
}

class _KaiBottomTipDialogState extends State<KaiBottomTipDialog> {
  Map<String, String> get stringMap {
    Map<String, String> result = {};
    WalletCheckStatus? walletStatus = WalletCheck().walletStatus;
    // ------------------  设置密码 提示 ---------------
    if (widget.type == DialogType.PASSWORD) {
      result['img_path'] = 'assets/images/dialog/risk.png';
      result['title'] = 'Create a Wallet Password';
      result['desc_1'] = 'Your wallet currently doesn\'t have any security';
      result['desc_2'] = 'protections. Please manage your wallet\'s';
      result['desc_3'] = 'security.';
      result['confirm_text'] = 'Confirm';
      result['cancel_text'] = 'Cancel';
    }
    // ------------------------ 风险 提示 ---------------------
    else if (widget.type == DialogType.RISK_SECURITY) {
      result['img_path'] = 'assets/images/dialog/crisk.png';
      result['title'] = 'Centralized Risk';
      result['desc_1'] = 'Your wallet currently doesn\'t have any security';
      result['desc_2'] = 'protections. Please manage your wallet\'s';
      result['desc_3'] = 'security.';
      result['confirm_text'] = 'Confirm';
      result['cancel_text'] = 'Cancel';
    }
    // 本地 分片 异常 单片分片
    else if (widget.type == DialogType.WALLET_ERROR) {
      result['img_path'] = 'assets/images/dialog/risk.png';
      result['title'] =
          'The Local files of your wallet may be lost or damaged. Please recast your MPC wallet using the dual-entropy increase confusion algorithm. Successful recasting will generate new key shards.';
      result['desc_1'] = '';
      result['desc_2'] = '';
      result['desc_3'] = '';
      result['confirm_text'] = 'Details';
      result['cancel_text'] = 'Cancel';
    }
    // 单片分片 服务端异常
    else if (widget.type == DialogType.SERVER_SIDE_ERROR) {
      result['img_path'] = 'assets/images/dialog/prisk.png';
      result['title'] =
          'The Server files of your wallet may be lost, damaged, or uploaded to iCloud. Please switch to an iCloud-supported device or recast your MPC wallet using the dual-entropy increase confusion algorithm. Successful recasting will generate new key shards.';
      result['desc_1'] = '';
      result['desc_2'] = '';
      result['desc_3'] = '';
      result['confirm_text'] = 'Details';
      result['cancel_text'] = 'Cancel';
    }
    // 单片分片 云端异常
    else if (widget.type == DialogType.CLOUD_SIDE_ERROR) {
      result['img_path'] = 'assets/images/dialog/risk.png';
      result['title'] =
          'The Cloud files of your wallet may be lost or damaged. Please recast your MPC wallet using the dual-entropy increase confusion algorithm. Successful recasting will generate new key shards.';
      result['desc_1'] = '';
      result['desc_2'] = '';
      result['desc_3'] = '';
      result['confirm_text'] = 'Details';
      result['cancel_text'] = 'Cancel';
    }
    // 双片 异常
    else if (widget.type == DialogType.DELETE_WALLET) {
      result['img_path'] = 'assets/images/dialog/risk.png';
      String text1 = 'Local';
      String text2 = 'Server';
      // 本地+服务  >  本地+云端  > 云端+服务
      if (walletStatus != null) {
        text1 = walletStatus.localpass ? (walletStatus.serverpass ? 'Cloud' : 'Server') : 'Local';
        text2 = walletStatus.serverpass ? (walletStatus.googlepass ? 'Local' : 'Cloud') : 'Server';
      }
      result['title'] =
          'Your ${text1} file and ${text2} file of wallet may be lost or damaged, your current device cannot use this wallet.';
      result['desc_1'] = 'Once the MPC wallet is destroyed, you can';
      result['desc_2'] = 'recreate a new one, but assets will be';
      result['desc_3'] = 'irrecoverable';
      result['confirm_text'] = 'Destroy Wallet';
      result['cancel_text'] = 'Cancel';
    }
    // 谷歌云授权
    else if (widget.type == DialogType.GOOGLE_CLOUD_AUTH) {
      result['img_path'] = 'assets/images/dialog/google_cloud.png';
      result['title'] = 'Your Google account has expired. Please reauthorize it.';
      result['desc_1'] = '';
      result['desc_2'] = '';
      result['desc_3'] = '';
      result['confirm_text'] = 'Authorize Google';
      result['cancel_text'] = 'Handle Later';
    }
    // 苹果云授权授权
    else if (widget.type == DialogType.APPLE_CLOUD_AUTH) {
      result['img_path'] = 'assets/images/dialog/apple_cloud.png';
      result['title'] = 'Your Apple account has expired. Please reauthorize it.';
      result['desc_1'] = '';
      result['desc_2'] = '';
      result['desc_3'] = '';
      result['confirm_text'] = 'Authorize Apple';
      result['cancel_text'] = 'Handle Later';
    }
    return result;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: EdgeInsets.only(left: 32.w, right: 32.w, bottom: 40.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.w), // 设置圆角大小为 20
        color: Colors.white,
      ),
      width: double.infinity,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(
            height: 10.w,
          ),
          PublicImage(
            image: stringMap['img_path']!,
            width: 120.w,
            height: 120.w,
          ),
          SizedBox(
            height: 10.w,
          ),
          Container(
            padding: EdgeInsets.only(left: 15.w, right: 15.w),
            child: Text(
              stringMap['title']!,
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontWeight: FontWeight.w500,
                  fontSize: 14.sp,
                  height: 20.w / 14.w,
                  color: Color.fromRGBO(51, 51, 51, 1)),
            ),
          ),
          Visibility(
              visible: widget.type == DialogType.PASSWORD ||
                  widget.type == DialogType.DELETE_WALLET ||
                  widget.type == DialogType.RISK_SECURITY,
              child: Container(
                padding: EdgeInsets.only(left: 15.w, right: 15.w),
                alignment: Alignment.center,
                child: Column(
                  children: [
                    SizedBox(
                      height: 10.w,
                    ),
                    Text(
                      stringMap['desc_1']!,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: 12.sp,
                          height: 16.w / 12.w,
                          color: Color.fromRGBO(134, 131, 145, 1)),
                    ),
                    Text(
                      stringMap['desc_2']!,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: 12.sp,
                          height: 16.w / 12.w,
                          color: Color.fromRGBO(134, 131, 145, 1)),
                    ),
                    Text(
                      stringMap['desc_3']!,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: 12.sp,
                          height: 16.w / 12.w,
                          color: Color.fromRGBO(134, 131, 145, 1)),
                    ),
                  ],
                ),
              )),
          SizedBox(
            height: 20.w,
          ),
          PublicButton(
            text: stringMap['confirm_text']!,
            type: widget.type == DialogType.DELETE_WALLET ? ButtonType.delete : ButtonType.private,
            margin: EdgeInsets.only(left: 25.w, right: 25.w),
            disabledStatus: false,
            onClick: () {
              Navigator.of(context).pop();
              if (widget.Confirm != null) widget.Confirm!();
            },
          ),
          SizedBox(
            height: 10.w,
          ),
          InkWell(
            onTap: () {
              Navigator.of(context).pop();
              if (widget.Cancel != null) widget.Cancel!();
            },
            splashColor: Colors.transparent, // 设置涟漪效果的颜色
            highlightColor: Colors.transparent, // 设置点击时的高亮颜色
            child: TextButton(
                onPressed: null,
                style: ButtonStyle(),
                child: Text(
                  stringMap['cancel_text']!,
                  style: TextStyle(
                      color: Color.fromRGBO(134, 131, 145, 1),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                      height: 20.w / 12.w),
                )),
          ),
          SizedBox(
            height: 10.w,
          ),
        ],
      ),
    );
  }
}
